package com.example.team.dao.Imp;

import com.example.team.dao.IEmployeeDao;
import com.example.team.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：沈超
 * 时间：2023/10/28
 */
@Component
public class IEmployeeDaoImpl implements IEmployeeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertEmployee(Employee employee) {
        return jdbcTemplate.update("insert into employee(Id, Name, Role, PartId, PostId, Sex, Age, Salary, Sort) " +
                "VALUE (?,?,?,?,?,?,?,?,?)"
                ,employee.getId()
                ,employee.getName()
                ,employee.getRole()
                ,employee.getPartid()
                ,employee.getPostid()
                ,employee.getSex()
                ,employee.getAge()
                ,employee.getSalary()
                ,employee.getSort());
    }

    @Override
    public int deleteEmployee(Employee employee) {
        return jdbcTemplate.update("delete from employee " +
                "where Id=?",employee.getId());
    }

    @Override
    public List<Employee> getAllEmployee(Employee employee) {
        return jdbcTemplate.query("select " +
                "Id,Name,Role,Partid,Postid,Sex,Age,Salary,Sort " +
                "from employee",new Object[]{},
                new BeanPropertyRowMapper<>(Employee.class));
    }

    @Override
    public int updateEmployee(Employee employee) {
        return jdbcTemplate.update("update employee set " +
                "Name=?,Role=?,Partid=?,Postid=?,Sex=?,Age=?,Salary=?,Sort=?" +"where Id=?"

                ,employee.getName()
                ,employee.getRole()
                ,employee.getPartid()
                ,employee.getPostid()
                ,employee.getSex()
                ,employee.getAge()
                ,employee.getSalary()
                ,employee.getSort()
                ,employee.getId());
    }

    @Override
    public Employee getEmployee(Employee employee) {
        List<Employee> result=jdbcTemplate.query("select " +
                        "Id,Name,Role,Partid,Postid,Sex,Age,Salary,Sort " +
                        "from employee where Id='"+employee.getId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Employee.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
